#include "curvedockwin.h"

CurveitemDockWin::CurveitemDockWin(QWidget *parent, QwtPlot *plot)
	:QDockWidget("Curve", parent)
{
	m_parent_plot = plot;

	// Dock windows
	setObjectName("curve_dock_win");
	setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
	setAllowedAreas(Qt::AllDockWidgetAreas);

	// Tree widget viewer
	m_curve_head = new QTreeWidgetItem();
	m_curve_head->setText(0, tr("curve"));
	m_curve_head->setTextAlignment(0, Qt::AlignLeft);
	m_curve_head->setCheckState(0, Qt::Checked);
	m_treeWidget = new QTreeWidget();
	m_treeWidget->setColumnCount(COLUMN_COUNT);
	m_treeWidget->setHeaderHidden(true);
	m_treeWidget->setColumnWidth(0, 80);
	m_treeWidget->setColumnWidth(1, 20);
	m_treeWidget->addTopLevelItem(m_curve_head);
	m_curve_head->setExpanded(true);
	
	/*
	QPixmap pixmap = QPixmap(CURVE_LENGEND_W, CURVE_LENGEND_H);
	pixmap.fill(m_color);
	setData(CURVE_LENGEND_INDEX, Qt::DecorationRole, pixmap);
	*/

	setWidget(m_treeWidget);
	connect(m_treeWidget, &QTreeWidget::itemChanged, this, &CurveitemDockWin::treeItemChanged);
	connect(m_treeWidget, &QTreeWidget::itemClicked, this, &CurveitemDockWin::on_itemClicked);

	m_standby_color_list.clear();
	// m_standby_color_list.append(QColor(Qt::black));
	m_standby_color_list.append(QColor(Qt::red));
	m_standby_color_list.append(QColor(Qt::darkRed));
	m_standby_color_list.append(QColor(Qt::green));
	m_standby_color_list.append(QColor(Qt::darkGreen));
	m_standby_color_list.append(QColor(Qt::blue));
	m_standby_color_list.append(QColor(Qt::darkBlue));
}

CurveitemDockWin::~CurveitemDockWin(void)
{
	qDebug() << "CurveitemDockWin close";
}

CurveItem *CurveitemDockWin::findCurveByName(QString tag)
{
	for (auto c : m_curveitem_list)
	{
		if (tag == c->getTag())
		{
			return c;
		}
	}

	return NULL;
}

QColor CurveitemDockWin::findUnsedCurveColor(void)
{
	m_standby_color_list.clear();
	// m_standby_color_list.append(QColor(Qt::black));
	m_standby_color_list.append(QColor(Qt::red));
	m_standby_color_list.append(QColor(Qt::darkRed));
	m_standby_color_list.append(QColor(Qt::green));
	m_standby_color_list.append(QColor(Qt::darkGreen));
	m_standby_color_list.append(QColor(Qt::blue));
	m_standby_color_list.append(QColor(Qt::darkBlue));

	for (auto c : m_curveitem_list)
	{
		for (auto u_c : m_standby_color_list)
		{
			if (c->getCurveColor() == u_c)
			{
				m_standby_color_list.removeOne(u_c);
			}
		}
	}

	if (m_standby_color_list.length())
	{
		return m_standby_color_list.at(0);
	}
	else
	{
		return QColor(Qt::darkGray);
	}
}

void CurveitemDockWin::addCurve(CurveItem *curve)
{
	m_curveitem_list.append(curve);
	m_curve_head->addChild((QTreeWidgetItem *)curve);
	curve->attachPlot(m_parent_plot);
}

void CurveitemDockWin::delAllCurvePoint(void)
{
	for (auto c : m_curveitem_list)
	{
		c->delPoint();
	}
}

int CurveitemDockWin::curveCount(void)
{
	return m_curveitem_list.length();
}

CurveItem *CurveitemDockWin::getCurveAt(qint32 pos)
{
	return m_curveitem_list.at(pos);
}

QList<CurveItem *> CurveitemDockWin::getCurveList(void)
{
	return m_curveitem_list;
}

void CurveitemDockWin::clearCurve(void)
{
	for (auto c : m_curveitem_list)
	{
		m_curve_head->removeChild(c);
		delete c;
	}
	m_curveitem_list.clear();
}

void CurveitemDockWin::treeItemChanged(QTreeWidgetItem* item, int column)
{
	qDebug() << item->text(0) << column;
	if (Qt::Checked == item->checkState(0))
	{
		int count = item->childCount();
		if (count >0)
		{
			for (int i = 0; i<count; i++)
			{
				item->child(i)->setCheckState(0, Qt::Checked);
			}
		}
		else
		{
			updateParentItem(item);
		}
	}
	else if (Qt::Unchecked == item->checkState(0))
	{
		int count = item->childCount();
		if (count > 0)
		{
			for (int i = 0; i<count; i++)
			{
				item->child(i)->setCheckState(0, Qt::Unchecked);
			}
		}
		else
		{
			updateParentItem(item);
		}
	}

	if (NULL != item->parent())
	{
		curveUpdateOnCheckChanged((CurveItem *)item);
	}
}

void CurveitemDockWin::updateParentItem(QTreeWidgetItem* item)
{
	QTreeWidgetItem *parent = item->parent();
	if (parent == NULL)
	{
		return;
	}
	int selectedCount = 0;
	int childCount = parent->childCount();
	for (int i = 0; i<childCount; i++)
	{
		QTreeWidgetItem* childItem = parent->child(i);
		if (childItem->checkState(0) == Qt::Checked)
		{
			selectedCount++;
		}
	}
	if (selectedCount <= 0)
	{
		parent->setCheckState(0, Qt::Unchecked);
	}
	else if (selectedCount>0 && selectedCount<childCount)
	{
		parent->setCheckState(0, Qt::PartiallyChecked);
	}
	else if (selectedCount == childCount)
	{
		parent->setCheckState(0, Qt::Checked);
	}
}

void CurveitemDockWin::curveUpdateOnCheckChanged(CurveItem *curve)
{
	if (Qt::Checked == curve->checkState(0))
	{
		curve->attachPlot(m_parent_plot);
	}
	else if (Qt::Unchecked == curve->checkState(0))
	{
		curve->detachPlot();
	}
	m_parent_plot->replot();
}

void CurveitemDockWin::on_itemClicked(QTreeWidgetItem *item, int column)
{
	CurveItem *curve = (CurveItem *)item;
	curve->onItemClicked(column);
}